package com.jzo2o.market.handler;

import com.jzo2o.market.service.ICouponService;
import com.jzo2o.market.service.ICouponUserService;
import com.jzo2o.redis.annotations.Lock;
import com.jzo2o.redis.constants.RedisSyncQueueConstants;
import com.jzo2o.redis.sync.SyncManager;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

import static com.jzo2o.market.constants.RedisConstants.Formatter.*;
import static com.jzo2o.market.constants.RedisConstants.RedisKey.COUPON_SEIZE_SYNC_QUEUE_NAME;

@Component
public class XxlJobHandler {

    @Resource
    private SyncManager syncManager;

    @Resource
    private ICouponService couponService;

    @Resource
    private ICouponUserService couponUserService;

    /**
     * 活动预热，将30天即将抢单及进行中的活动进行缓存  一天一次
     *
     */
    @XxlJob("activityPreheat")
    @Lock(formatter = ACTIVITY_PREHEAT, startDog = true)
    public void activityPreHeat() {
        couponService.preHeat();
    }

    /**
     * 抢券同步队列
     * 5s一次
     */
    @XxlJob("seizeCouponSyncJob")
    @Lock(formatter = COUPON_SEIZE_HANDLE_LOCK, startDog = true)
    public void seizeCouponSyncJob() {
        syncManager.start(COUPON_SEIZE_SYNC_QUEUE_NAME, RedisSyncQueueConstants.STORAGE_TYPE_HASH, RedisSyncQueueConstants.MODE_SINGLE);
    }
    /**
     * 活动状态修改，
     * 1.活动进行中状态修改
     * 2.活动已失效状态修改
     * 1分钟一次
     */
    @XxlJob("updateActivityStatus")
    public void updateActivitySatus(){
        couponService.updateStatus();
    }
    /**
     * 活动结束 处理任务 一天执行一次
     * 1.删除结束活动的库存
     * 2.删除结束活动的抢券列表
     *
     */
    @XxlJob("activityFinish")
    @Lock(formatter = ACTIVITY_FINISHED, startDog = true)
    public void activityFinished() {
        couponService.activityFinished();
    }

    @XxlJob("processExpireCoupon")
    public void processExpireCoupon() {
        couponUserService.processExpireCoupon();
    }


}
